<?php
class NotaFiscai extends AppModel {
	var $name = 'NotaFiscai';
	var $primaryKey = 'id_nota_fiscal';
	var $validate = array(
		'id_nota_fiscal' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'user_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'date_data' => array(
			'date' => array(
				'rule' => array('date'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'fornecedore_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'message' => 'Escolha um fornecedor',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Escolha um fornecedor',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'numero_nota' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'message' => 'Número da nota deve ser um número',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Número da nota não pode ser branco',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'money_valor' => array(
			'numeric' => array(
				'rule' => "/[0-9]*[\.,][0-9]{2}/",
				'message' => 'Valor da nota inválido',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Valor da nota não pode ser vazia',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'money_IPI' => array(
			'numeric' => array(
				'rule' => "/[0-9]*[\.,][0-9]{2}/",
				'message' => 'IPI inválido',
				'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'money_desconto' => array(
			'numeric' => array(
				'rule' => "/[0-9]*[\.,][0-9]{2}/",
				'message' => 'Desconto inválido',
				'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Fornecedore' => array(
			'className' => 'Fornecedore',
			'foreignKey' => 'fornecedore_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	var $hasMany = array(
		'Movimento' => array(
			'className' => 'Movimento',
			'foreignKey' => 'nota_fiscal_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
	
	function afterFind($results){
		foreach ($results as &$item){
			if(isset($item['NotaFiscai']['date_data'])){
				$item['NotaFiscai']['date_data_view'] = date("d/m/Y", strtotime($item['NotaFiscai']['date_data']));
			}
			if(isset($item['NotaFiscai']['money_valor']))
				$item['NotaFiscai']['money_valor_view'] = 'R$ '.number_format($item['NotaFiscai']['money_valor']/100, 2, ',',' ');
			if(isset($item['NotaFiscai']['money_IPI'])){
				if(!empty($item['NotaFiscai']['money_IPI'])){
					$item['NotaFiscai']['money_IPI_view'] = 'R$ '.number_format($item['NotaFiscai']['money_IPI']/100, 2, ',',' ');
				}
				else{ $item['NotaFiscai']['money_IPI_view'] = 'R$ 0,00';}
			}
			if(isset($item['NotaFiscai']) && !isset($item['NotaFiscai']['money_IPI']) && is_array($item['NotaFiscai'])){
					$item['NotaFiscai']['money_IPI_view'] = 'R$ 0,00';
			}
			if(isset($item['NotaFiscai']['money_desconto']))
				$item['NotaFiscai']['money_desconto_view'] = 'R$ '.number_format($item['NotaFiscai']['money_desconto']/100, 2, ',',' ');
		}
		return $results;
	}
	
	function beforeSave($options){
		$pontoVirgula = array('.',',');
		$this->data['NotaFiscai']['money_valor'] = str_replace($pontoVirgula, "", $this->data['NotaFiscai']['money_valor']);
		$this->data['NotaFiscai']['money_desconto'] = str_replace($pontoVirgula, "", $this->data['NotaFiscai']['money_desconto']);
		$this->data['NotaFiscai']['money_IPI'] = str_replace($pontoVirgula, "", $this->data['NotaFiscai']['money_IPI']);
		return true;
	}

}
?>